﻿using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFStudy.PrismStudy.Views;

namespace WPFStudy.PrismStudy
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        IContainerExtension _containerExtension;
        IRegionManager _regionManager;
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 根据Prism Ioc 注入、解析相关对象
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="containerManager"></param>
        public MainWindow(IRegionManager regionManager, IContainerExtension containerManager) : this()
        {
            _containerExtension = containerManager;
            _regionManager = regionManager;
        }

        private void btnRegion_Click(object sender, RoutedEventArgs e)
        {
            //RegionStudyView 是一个window
            RegionStudyView view = new RegionStudyView();

            #region 为了处理Region在自己新打开的Window中失效，需要在RegionManager中将Window设为附加属性，并更新
            //参照原文：https://www.cnblogs.com/zh7791/p/15801707.html

            // _containerExtension.Resolve<IRegionManager>() 可替换为
            //var regionManager = ContainerLocator.Container.Resolve<IRegionManager>();
            RegionManager.SetRegionManager(view, _containerExtension.Resolve<IRegionManager>());
            RegionManager.UpdateRegions();

            view.Closed += (s, e) =>
            {
                //如果在弹窗服务当中设置区域, 在窗口再次打开的同时会提示区域名称已注册的问题, 则你需要在关闭前移除原有的所有区域名即可
                var regionManager = ContainerLocator.Container.Resolve<IRegionManager>();
                //`ContentRegion` 为 RegionStudyView 中的 RegionName
                regionManager.Regions.Remove("ContentRegion");
            };
            #endregion

            view.ShowDialog();
        }

        private void btnModule_Click(object sender, RoutedEventArgs e)
        {
            //RegionStudyView 是一个window
            var regionManager = ContainerLocator.Container.Resolve<IRegionManager>();
            ModuleStudyView view = new ModuleStudyView(regionManager);
            #region 为了处理Region在自己新打开的Window中失效，需要在RegionManager中将Window设为附加属性，并更新
            //参照原文：https://www.cnblogs.com/zh7791/p/15801707.html
            RegionManager.SetRegionManager(view, _containerExtension.Resolve<IRegionManager>());
            RegionManager.UpdateRegions();

            view.Closed += (s, e) =>
            {
                //如果在弹窗服务当中设置区域, 在窗口再次打开的同时会提示区域名称已注册的问题, 则你需要在关闭前移除原有的所有区域名即可                
                //`ModuleStudyContentRegion` 为 ModuleStudyView 中的 RegionName
                regionManager.Regions.Remove("ModuleStudyContentRegion");
            };
            #endregion

            view.ShowDialog();

        }

        private void btnNavigation_Click(object sender, RoutedEventArgs e)
        {
            //RegionStudyView 是一个window
            var regionManager = _containerExtension.Resolve<IRegionManager>();
            var view = new NavigateView();
            #region 为了处理Region在自己新打开的Window中失效，需要在RegionManager中将Window设为附加属性，并更新
            //参照原文：https://www.cnblogs.com/zh7791/p/15801707.html
            RegionManager.SetRegionManager(view, _containerExtension.Resolve<IRegionManager>());
            RegionManager.UpdateRegions();

            view.Closed += (s, e) =>
            {
                //如果在弹窗服务当中设置区域, 在窗口再次打开的同时会提示区域名称已注册的问题, 则你需要在关闭前移除原有的所有区域名即可                
                //`ModuleStudyContentRegion` 为 ModuleStudyView 中的 RegionName
                regionManager.Regions.Remove("ContentRegionNavigate");
            };
            #endregion

            view.ShowDialog();

        }

        private void btnDialog_Click(object sender, RoutedEventArgs e)
        {
            new DialogView().ShowDialog();
        }

        private void btnPubSubEvent_Click(object sender, RoutedEventArgs e)
        {
            new PubSubEventView().ShowDialog();
        }
    }
}
